package hibernate.method;

import hibernate.constraint.ErrorCodes;

import java.io.File;
import java.io.FileInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.validator.*;
import org.joda.time.LocalDate;
import org.joda.time.Years;

public class AdditionalMethods {

	public static String getCurrentDate() {
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		return date.format(new Date());
	}
	
	@SuppressWarnings("deprecation")
	public static Years calMyAge(String d) throws ParseException { 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = formatter.parse(d);
		LocalDate birthdate = new LocalDate(date.getYear(), date.getMonth()+1, date.getDay());
    	LocalDate now = new LocalDate(new Date().getYear(), new Date().getMonth()+1, new Date().getDay());
    	Years age = Years.yearsBetween(birthdate, now);
    	
    	return age;
	}

	public static String getCurrentTime() {
		SimpleDateFormat time = new SimpleDateFormat("kk:mm:ss");
		return time.format(new Date());
	}
	
	public static byte[] setDefaultUserAvatar(String gender){
		File file = null;
		if (gender=="male"){
	file = new File("../images/avatar_male.jpg");}
		else if(gender=="female"){
	file = new File("../images/avatar_female.jpg");
		}
	byte[] bFile = new byte[(int) file.length()];

	try {
		FileInputStream fileInputStream = new FileInputStream(file);
		fileInputStream.read(bFile);
		fileInputStream.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return bFile;
	}
	
	public static int email_validation(String password) { 
		@SuppressWarnings("deprecation")
		boolean matchFound = EmailValidator.getInstance().isValid(password);

		if (matchFound) {
		     return 0;
		} else {
		     return ErrorCodes.EMAIL_INVALID;
		}
	}
}
